package com.bng8.superstar_cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bng8.superstar_cms.domain.dto.SupChallengerDTO;
import com.bng8.superstar_cms.domain.entity.SupChallengerDO;
import com.bng8.superstar_cms.mapper.SupChallengerMapper;
import com.bng8.superstar_cms.service.SupChallengerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 20393
 * @description 针对表【sup_challenger(挑战者表)】的数据库操作Service实现
 * @createDate 2023-08-17 11:22:14
 */
@Service
public class SupChallengerServiceImpl extends ServiceImpl<SupChallengerMapper, SupChallengerDO>
        implements SupChallengerService {
    @Autowired
    private SupChallengerMapper challengerMapper;

    private SupChallengerDO challengerDO;

    private List<SupChallengerDO> supChallengerDOS;

    /**
     * 新增挑战者
     *
     * @param request
     * @param challengerDTO
     * @return
     */
    @Override
    public int save(HttpServletRequest request, SupChallengerDTO challengerDTO) {
        List<SupChallengerDO> supChallengerDOS =
                challengerMapper.selectList(new QueryWrapper<SupChallengerDO>().eq("user_name", challengerDTO.getUserName()).eq("challenger", challengerDTO.getChallenger()));

        if (supChallengerDOS.size() != 0) return -1;

        BeanUtils.copyProperties(challengerDTO, challengerDO);

        if (challengerDTO.getMission() != null) {
            challengerDO.setMission(challengerDTO.getMission().getMissionName());
        }
        challengerDO.setCreator(request.getRemoteAddr());
        challengerDO.setOperator(request.getRemoteAddr());
        return challengerMapper.insert(challengerDO);
    }

    /**
     * 删除挑战者
     *
     * @param id
     * @return
     */
    @Override
    public int deleter(HttpServletRequest request, Long id) {
        challengerDO = challengerMapper.selectById(id);
        // 判断id是否存在
        if (challengerDO != null) {
            challengerDO.setOperator(request.getRemoteAddr());
            return challengerMapper.deleteById(id);
        }
        return -1;
    }

    @Override
    public List<SupChallengerDTO> queryChallengerByUserId() {
        /*ArrayList<Long> idList = new ArrayList<>();
        idList.add(id);
        supChallengerDOS = challengerMapper.selectBatchIds(idList);*/
        supChallengerDOS = challengerMapper.selectList(null);
        // 对数据进行转换
        return supChallengerDOS.stream()
                .map(challengerDO -> {
                    SupChallengerDTO supChallengerDTO = new SupChallengerDTO();
                    BeanUtils.copyProperties(challengerDO, supChallengerDTO);
                    return supChallengerDTO;
                }).collect(Collectors.toList());
    }
}
